package com.gzhryc.shared_device.oem.h5.mng.controllers.user;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.jdbc.DateBranchHelper;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserWallet;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserWalletOperate;
import com.gzhryc.shared_device.oem.code.user.dao.enums.UserTransferMoneyOrderEnum;
import com.gzhryc.shared_device.oem.code.user.services.UserService;
import com.gzhryc.shared_device.oem.code.user.services.UserTransferMoneyOrderService;
import com.gzhryc.shared_device.oem.code.user.services.UserWalletOperateService;
import com.gzhryc.shared_device.oem.code.user.services.UserWalletService;
import com.gzhryc.shared_device.oem.code.user.services.dto.UserWalletOperateSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.List;

public class UserWalletController {

    @Route(value = {"/user/wallet/info.html"})
    public String info(HttpServletRequest request) {
        Long id = WebUtils.getLong(request, "id");
        User user = UserService.self().getById(id);
        if (user != null) {
            WebUtils.setAttribute(request, user);
            List<UserWallet> walletList = UserWalletService.self().findJoinByUserId(user.getId());
            if(walletList != null) {
                request.setAttribute("walletList", walletList);
            }

            Integer transferMoney = UserTransferMoneyOrderService.self().sumTransferMoneyByUserIdAndState(user.getId(), UserTransferMoneyOrderEnum.State.Success.index());
            request.setAttribute("transferMoneyStr", NumberTools.getMoneyStr(transferMoney));
        }
        return "/user/wallet/info.html";
    }

    @Route(value = {"/user/wallet/withdrawal_money/transfer.html"})
    public String transfer(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request, "userId");
        Long accountGroupId = WebUtils.getLong(request,"accountGroupId");
        UserWallet wallet = UserWalletService.self().getByKeys(userId,accountGroupId);
        WebUtils.setAttribute(request,wallet);
        return "/user/wallet/transfer.html";
    }

    @Route(value = {"/user/wallet/withdrawal_money/transfer.do"})
    public AjaxResult doTransfer(HttpServletRequest request, HttpSession session){
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"转出失败");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        Long userId = WebUtils.getLong(request, "userId");
        Long accountGroupId = WebUtils.getLong(request, "accountGroupId");
        Double money = WebUtils.getDouble(request,"money");
        String operateNote = WebUtils.getString(request,"operateNote");


        OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
        operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
        operateInfo.setOperateNote(operateNote);

        try {
            if(UserWalletService.self().transferMoneyToWithdrawal(userId,accountGroupId,NumberTools.changeMoneyLi(money),operateInfo)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
    
    /**
     * 从提现到余额
     * @param request
     * @return
     */
    @Route(value = {"/user/wallet/withdrawal_money/restitution.html"})
    public String restitution(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request, "userId");
        Long accountGroupId = WebUtils.getLong(request,"accountGroupId");
        UserWallet wallet = UserWalletService.self().getByKeys(userId,accountGroupId);
        WebUtils.setAttribute(request,wallet);
        return "/user/wallet/restitution.html";
    }

    @Route(value = {"/user/wallet/withdrawal_money/restitution.do"})
    public AjaxResult doRestitution(HttpServletRequest request, HttpSession session){
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"转入失败");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        Long userId = WebUtils.getLong(request, "userId");
        Long accountGroupId = WebUtils.getLong(request, "accountGroupId");
        Double money = WebUtils.getDouble(request,"money");
        String operateNote = WebUtils.getString(request,"operateNote");


        OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
        operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
        operateInfo.setOperateNote(operateNote);

        try {
            if(UserWalletService.self().restitutionToMoney(userId,accountGroupId,NumberTools.changeMoneyLi(money),operateInfo)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = {"/user/wallet/operate/list.html"})
    public String operateList(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request, "userId");
        Long accountGroupId = WebUtils.getLong(request,"accountGroupId");
        UserWallet wallet = UserWalletService.self().getJoinByKeys(userId,accountGroupId);
        WebUtils.setAttribute(request,wallet);
        request.setAttribute("dateBranchItemList", DateBranchHelper.self().getMonthDateBranchItems());
        return "/user/wallet/operate_record.html";
    }

    @Route(value = "/user/wallet/operate/list.data")
    public ListResult<UserWalletOperate> operateListDate(HttpServletRequest request) {
        ListResult<UserWalletOperate> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        UserWalletOperateSearch search = WebUtils.getParameter(request,UserWalletOperateSearch.class);
        if(search.getUserId() != null && search.getAccountGroupId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
            
            Date dateBranch = new Date();
            if(StringTools.isNotBlank(search.getDateBranchId())){
                dateBranch = DateBranchHelper.getDateByItemId(search.getDateBranchId());
            }

            result.getBody().setData(UserWalletOperateService.self(dateBranch).findBySearch(search,pageNo,pageLimit));
            if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }
}
